<template>
  <el-dialog :title="$t('tool.modelManage.' + title)" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-position="top">
      <el-form-item :label="$t('tool.modelManage.dllNumber')" prop="dllNumber">
        <el-input v-model="form.dllNumber" autocomplete="off"
          placeholder="v1.0.1,   版本名称不能重复"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tool.modelManage.dllDescription')" prop="dllDescription">
        <el-input v-model="form.dllDescription" autocomplete="off"
          :placeholder="$t('tool.others.placehoderInput')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tool.modelManage.dllFilePath')" prop="dllFilePath">
        <div class="flex flex-row justify-center">
          <el-input ref="dllFilePath" v-model="form.dllFilePath" autocomplete="off" readonly
            :placeholder="$t('tool.others.placehoderUpload')"></el-input>
          <el-button @click="handleUpload($event, 'agent')" style="margin-left: 5px">{{ $t("setup.buttonText.upload")
            }}</el-button>
        </div>
      </el-form-item>
      <el-form-item :label="$t('tool.modelManage.codeFilePath')" prop="codeFilePath">
        <div class="flex flex-row justify-center">
          <el-input ref="codeFilePath" v-model="form.codeFilePath" autocomplete="off" readonly
            :placeholder="$t('tool.others.placehoderUpload')"></el-input>
          <el-button @click="handleUpload($event, 'code')" style="margin-left: 5px">{{ $t("setup.buttonText.upload")
            }}</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button class="jtyj-btn-cancel" @click="cancel">{{
        $t("setup.buttonText.cancel")
      }}</el-button>
      <el-button class="jtyj-btn-primary" @click="submitForm">{{
        $t("setup.buttonText.save")
      }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from "@/utils/auth";
import {
  addDll,
  updateDll,
} from "@/api/myspace/model-manage-tool/model-manage";
import { mapState } from "vuex";

export default {
  data() {
    return {
      title: "",
      mainVersionId: null,
      dialogFormVisible: false,
      form: {},
      uploadFlag: {
        code: false,
        agent: false,
      },
      rules: {
        dllNumber: [
          { required: true, message: "版本号不得为空", trigger: "blur" },
        ],
        dllDescription: [
          { required: true, message: "描述不得为空", trigger: "blur" },
        ],
        codeFilePath: [
          { required: true, message: "请上传动态链接库文件", trigger: "blur" },
        ],
        dllFilePath: [
          { required: true, message: "请上传源码文件", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapState({
      userId: (state) => state.user.id,
    }),
  },
  mounted() {
    // 文件上传成功的回调
    this.$EventBus.$on("fileSuccess", (response) => {
      if (response.code == 200) {
        if (this.uploadFlag.code) {
          // this.form.codeFilePath = response.data.filePath;
          this.$set(this.form, "codeFilePath", response.data.filePath);
          this.uploadFlag = { code: false, agent: false };
        } else if (this.uploadFlag.agent) {
          // this.form.dllFilePath = response.data.filePath;
          this.$set(this.form, "dllFilePath", response.data.filePath);
          this.uploadFlag = { code: false, agent: false };
        }
      } else {
        this.uploadFlag = { code: false, agent: false };
        this.$message.error(
          this.$toolHandle.getI18nName(
            this.$zh.setup.responseText,
            this.$en.setup.responseText,
            this.$i18n.locale,
            "error"
          )
        );
      }
      this.$EventBus.$emit("close");
    });
  },
  methods: {
    init(row, mainversionId) {
      this.dialogFormVisible = true;
      this.mainVersionId = mainversionId;
      this.reset();
      if (!row) {
        this.title = "editDll";
      } else {
        this.title = "addMainVersion";
        this.form = JSON.parse(JSON.stringify(row));
      }
    },
    handleUpload(e, type) {
      if (
        Object.values(this.uploadFlag).filter((item) => item == true).length > 0
      )
        return;
      this.uploadFlag[type] = true;
      this.$EventBus.$emit("openUploader", {
        token: getToken(),
        prefixFlag: 2,
        userId: this.userId,
      });
    },
    reset() {
      this.form = {
        dllNumber: null,
        dllDescription: null,
        codeFilePath: null,
        dllFilePath: null,
        userId: this.userId,
      };
    },
    cancel() {
      this.dialogFormVisible = false;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.form.dllId) {
            addDll({
              dllNumber: this.form.dllNumber,
              dllDescription: this.form.dllDescription,
              codeFilePath: this.form.codeFilePath,
              dllFilePath: this.form.dllFilePath,
              userId: this.userId,
              mainVersionId: this.mainVersionId,
            }).then((response) => {
              if (response.code == 200) {
                this.$message.success(
                  this.$toolHandle.getI18nName(
                    this.$zh.setup.responseText,
                    this.$en.setup.responseText,
                    this.$i18n.locale,
                    "success"
                  )
                );
                this.$emit("refresh");
                this.dialogFormVisible = false;
              }
            });
          } else {
            updateDll({
              dllId: this.form.dllId,
              dllNumber: this.form.dllNumber,
              dllDescription: this.form.dllDescription,
              codeFilePath: this.form.codeFilePath,
              dllFilePath: this.form.dllFilePath,
              userId: this.userId,
              mainVersionId: this.mainVersionId,
            }).then((response) => {
              if (response.code == 200) {
                this.$message.success(
                  this.$toolHandle.getI18nName(
                    this.$zh.setup.responseText,
                    this.$en.setup.responseText,
                    this.$i18n.locale,
                    "success"
                  )
                );
                this.$emit("refresh");
                this.dialogFormVisible = false;
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style></style>